package com.pn.taikao.dto;

import lombok.Data;

/**
 * 统一API响应格式
 * @param <T> 数据类型
 */
@Data
public class ApiResponse<T> {

    private int code;

    private String msg;

    private T data;

    public static <T> ApiResponse<T> success(T data) {
        ApiResponse<T> r = new ApiResponse<>();
        r.code = 0;
        r.msg = "success";
        r.data = data;
        return r;
    }

    public static <T> ApiResponse<T> error(String msg) {
        ApiResponse<T> r = new ApiResponse<>();
        r.code = 1;
        r.msg = msg;
        r.data = null;
        return r;
    }

    public static <T> ApiResponse<T> error(int code, String msg) {
        ApiResponse<T> r = new ApiResponse<>();
        r.code = code;
        r.msg = msg;
        r.data = null;
        return r;
    }
}

